package com.chunfeng.lerunapplication.fragement;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.chunfeng.lerunapplication.R;
import com.chunfeng.lerunapplication.grid.GridAdapt;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.Arrays;
import java.util.List;


public class HomeFragment extends Fragment {
    /**
     * 轮播图组件
     */
    private Banner banner;
    /**
     * 图片资源
     */
    private List<Integer> images = Arrays.asList(
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3
    );
    /**
     * 九宫格
     */
    private GridView table;

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 渲染视图并初始化组件
     *
     * @param inflater           布局反射器
     * @param container          父容器
     * @param savedInstanceState 状态
     * @return 布局对象
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        banner = view.findViewById(R.id.main_banner);
        table = view.findViewById(R.id.Table_Image);
        initBanner();
        return view;
    }

    /**
     * 初始化轮播图图片资源
     */
    public void initBanner() {
        //绑定数据
        banner.setAdapter(new BannerImageAdapter<Integer>(images) {
            /**
             * 绑定布局数据
             *
             * @param holder   XViewHolder
             * @param data     数据实体
             * @param position 当前位置
             * @param size     总数
             */
            @Override
            public void onBindView(BannerImageHolder holder, Integer data, int position, int size) {
                holder.imageView.setImageResource(data);
            }
        });
        // 开启循环轮播
        banner.isAutoLoop(true);
        banner.setIndicator(new CircleIndicator(getContext()));
        banner.setScrollBarFadeDuration(1000);
        banner.setIndicatorSelectedColor(Color.GREEN);
        // 开始轮播
        banner.start();
        table.setAdapter(new GridAdapt());
        //设置列数
        table.setNumColumns(4);
        //设置垂直间距
        table.setVerticalSpacing(30);
    }
}